/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mujmail;
import lcduilwuit.*;
import mujmail.util.Functions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.Dialog;

/**
 *
 * @author anup.s
 */

  public class ClearDBSelect extends Form implements ActionListener{
        private static final byte CLR_DB_INBOX       = 0;
        private static final byte CLR_DB_OUTBOX      = CLR_DB_INBOX + 1;
        private static final byte CLR_DB_DRAFT       = CLR_DB_OUTBOX + 1;
        private static final byte CLR_DB_SENTBOX     = CLR_DB_DRAFT + 1;
        private static final byte CLR_DB_TRASH       = CLR_DB_SENTBOX + 1;
        //#ifdef MUJMAIL_USR_FOLDERS
        private static final byte CLR_DB_USERFOLDERS = CLR_DB_TRASH + 1;
        //#else
//#         private static final byte CLR_DB_USERFOLDERS = CLR_DB_TRASH;
        //#endif
        private static final byte CLR_DB_ACCOUNTS    = CLR_DB_USERFOLDERS + 1;
        private static final byte CLR_DB_ADRESSBOOX  = CLR_DB_ACCOUNTS + 1;
        private static final byte CLR_DB_MSGIDS      = CLR_DB_ADRESSBOOX + 1;
        private static final byte CLR_DB_SETTINGS      = CLR_DB_MSGIDS + 1;
        private static final byte CLR_DB_SIZE        = CLR_DB_SETTINGS + 1; // Lease this as last element

        private ChoiceGroup DB_to_delete;
        private Command OK, cancel;

        public ClearDBSelect() {
            super(Lang.get(Lang.AC_CLEAR_DB));

            DB_to_delete = new ChoiceGroup(/*Lang.get(Lang.DB_SPACE_LEFT)*/"", Choice.MULTIPLE);
            DB_to_delete.append(Lang.get(Lang.TB_INBOX), null);
            DB_to_delete.append(Lang.get(Lang.TB_OUTBOX), null);
            DB_to_delete.append(Lang.get(Lang.TB_DRAFT), null);
            DB_to_delete.append(Lang.get(Lang.TB_SENTBOX), null);
            DB_to_delete.append(Lang.get(Lang.TB_TRASH), null);
            //#ifdef MUJMAIL_USR_FOLDERS
            DB_to_delete.append(Lang.get(Lang.EXTRA_TB_USERFOLDERS), null);
            //#endif
            DB_to_delete.append(Lang.get(Lang.AS_ACCOUNTS), null);
            DB_to_delete.append(Lang.get(Lang.AD_ADDRESSBOOK), null);
            DB_to_delete.append(Lang.get(Lang.MSGIDS_CACHE), null);
            DB_to_delete.append(Lang.get(Lang.EXTRA_SETTINGS), null);
            DB_to_delete.setSelectedFlags(new boolean[CLR_DB_SIZE]); //set all to false
            append(DB_to_delete);

            OK = new Command(Lang.get(Lang.BTN_OK), Command.OK, 0);
            cancel = new Command(Lang.get(Lang.BTN_CANCEL), Command.BACK, 0);
            addCommand(cancel);
            addCommand(OK);
            setCommandListener(this);
        }

    public void actionPerformed(ActionEvent evt) {
         if (evt.getCommand() == OK) {
                clearDataBases(false, false);
            } else {
               MujMail.mujmail.mainMenu();
            }
    }

        public void refresh() {
         //   DB_to_delete.setLabel(Lang.get(Lang.DB_SPACE_LEFT) + Functions.spaceLeft("SPACE_TEST") / 1024 + "kB");
            int spaceOccupied = MujMail.mujmail.getInBox().getOccupiedSpace();
            DB_to_delete.set(CLR_DB_INBOX, Lang.get(Lang.TB_INBOX) + " (" + spaceOccupied / 1024 + "kB)", null);

            spaceOccupied = MujMail.mujmail.outBox.getOccupiedSpace();
            DB_to_delete.set(CLR_DB_OUTBOX, Lang.get(Lang.TB_OUTBOX) + " (" + spaceOccupied / 1024 + "kB)", null);

            spaceOccupied = MujMail.mujmail.draft.getOccupiedSpace();
            DB_to_delete.set(CLR_DB_DRAFT, Lang.get(Lang.TB_DRAFT) + " (" + spaceOccupied / 1024 + "kB)", null);

            spaceOccupied = MujMail.mujmail.getSentBox().getOccupiedSpace();
            DB_to_delete.set(CLR_DB_SENTBOX, Lang.get(Lang.TB_SENTBOX) + " (" + spaceOccupied / 1024 + "kB)", null);

            spaceOccupied = MujMail.mujmail.getTrash().getOccupiedSpace();
            DB_to_delete.set(CLR_DB_TRASH, Lang.get(Lang.TB_TRASH) + " (" + spaceOccupied / 1024 + "kB)", null);

            //#ifdef MUJMAIL_USR_FOLDERS
            spaceOccupied = MujMail.mujmail.getUserMailBoxes().spaceOccupied();
            DB_to_delete.set(CLR_DB_USERFOLDERS, Lang.get(Lang.EXTRA_TB_USERFOLDERS) + " (" + spaceOccupied / 1024 + "kB)", null);
            //#endif

            spaceOccupied = Functions.spaceOccupied("ACCOUNTS");
            DB_to_delete.set(CLR_DB_ACCOUNTS, Lang.get(Lang.AS_ACCOUNTS) + " (" + spaceOccupied / 1024 + "kB)", null);

            spaceOccupied = Functions.spaceOccupied("AddressBook");
            DB_to_delete.set(CLR_DB_ADRESSBOOX, Lang.get(Lang.AD_ADDRESSBOOK) + " (" + spaceOccupied / 1024 + "kB)", null);

            spaceOccupied = MujMail.mujmail.getMailDBSeen().getOccupiedSpace();
            DB_to_delete.set(CLR_DB_MSGIDS, Lang.get(Lang.MSGIDS_CACHE) + " (" + spaceOccupied / 1024 + "kB)", null);

            DB_to_delete.set(CLR_DB_SETTINGS, Lang.get(Lang.EXTRA_SETTINGS), null);

        }

        /**
         * @param sure
         * @param deleteAll deletes all settings independently on which was selected.
         */
        public void clearDataBases(boolean sure, boolean deleteAll) {
            boolean[] selected = new boolean[CLR_DB_SIZE];
            if (!deleteAll && DB_to_delete.getSelectedFlags(selected) == 0) {
               MujMail.mujmail.mainMenu();
                return;
            }

            if (!sure) {
               /* alert.setAlert(this, getMenu(),Lang.get(Lang.ALRT_SYS_DEL_ALL_CONFIRM), MyAlert.DB_CLEAR_CONFIRM, AlertType.CONFIRMATION);
                return;*/
                if(Dialog.show(Lang.get(Lang.EXTRA_SONIMAIL),Lang.get(Lang.ALRT_SYS_DEL_ALL_CONFIRM),Lang.get(Lang.BTN_OK),Lang.get(Lang.BTN_CANCEL)))
                    clearDataBases(true, false);
                 return;
            }

            if (deleteAll || selected[CLR_DB_INBOX]) {
                MujMail.mujmail.getInBox().deleteAllMailsFromBoxAndDB(true);
            }
            if (deleteAll || selected[CLR_DB_OUTBOX]) {
                MujMail.mujmail.getOutBox().deleteAllMailsFromBoxAndDB(true);
            }
            if (deleteAll || selected[CLR_DB_DRAFT]) {
                MujMail.mujmail.getDraft().deleteAllMailsFromBoxAndDB(true);
            }
            if (deleteAll || selected[CLR_DB_SENTBOX]) {
                MujMail.mujmail.getSentBox().deleteAllMailsFromBoxAndDB(true);
            }
            if (deleteAll || selected[CLR_DB_TRASH]) {
                MujMail.mujmail.getTrash().deleteAllMailsFromBoxAndDB(true);
            }
            //#ifdef MUJMAIL_USR_FOLDERS
            if (deleteAll || selected[CLR_DB_USERFOLDERS]) {
                MujMail.mujmail.getUserMailBoxes().deleteAllMailsFromAllUserBoxesAndDB();
            }
            //#endif

            if (deleteAll || selected[CLR_DB_ACCOUNTS]) {
                MujMail.mujmail.getAccountSettings().deleteAll(true);
            }
            if (deleteAll || selected[CLR_DB_ADRESSBOOX]) {
               //MujMail.mujmail.getAddressBook().deleteAll(true);
                AddressBook.deleteAll(true);
            }
            if (deleteAll || selected[CLR_DB_MSGIDS]) {
                MujMail.mujmail.getMailDBSeen().deleteAll(true);
            }
            if (deleteAll || selected[CLR_DB_SETTINGS]) {
                MujMail.mujmail.getSettings().restoreSettings();
            }

            DB_to_delete.setSelectedFlags(new boolean[CLR_DB_SIZE]); //set all to false

               MujMail.mujmail.mainMenu();
        }

    }

